home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0277.ZIP / WILDMSD2.C < prev    next >
Text File  |  1985-08-04  |  5KB  |  220 lines

  1. /*
  2. WILDEXP.C
  3.  
  4.      C86 C version of wildexp.c - thanks to
  5.      Leor Zolman for original BDS C version.
  6.  
  7.      26.08.84 - C86 version for DOS 2.xx
  8.  
  9. */
  10.  
  11. #include    <stdio.h>
  12.  
  13. #define     FNSZE        80    /* maximum filename size */
  14. #define     ERROR        -1
  15. #define     MAXITEMS    500    /* max no. of items after expansion */
  16. #define     SEARCH_FIRST    17    /* BDOS calls */
  17. #define     SEARCH_NEXT    18
  18. #define     TRUE        1
  19. #define     FALSE        0
  20.  
  21. struct regval { unsigned ax, bx, cx, dx, si, di, ds, es; };
  22. struct sval { unsigned css, sss, dss, ess; };
  23.  
  24. wildexp(oargcp, oargvp, atts)
  25. int    *oargcp;        /* pointer to old argc */
  26. char    ***oargvp;        /* pointer to old argv */
  27. int    atts;            /* desired file attributes */
  28. {
  29.     int    nargc;        /* new argc */
  30.     char    **nargv;    /* new argv */
  31.     char    **oargv;    /* old argv */
  32.     int    oargc;        /* old argc */
  33.     int    dmapos;     /* value returned by search calls */
  34.     char    first_time;    /* used in search routine */
  35.     char    tmpfn[FNSZE],*tmpfnp;
  36.     int    new_c;
  37.     int    i,j,k;
  38.     char    c;
  39.     char    *sbrk();
  40.     char    cur_dir[68], root_dir[5];
  41.     char    fd[FNSZE], dta[128];
  42.     char    *strrchr();
  43.     struct    regval regs;
  44.     struct    sval segs;
  45.  
  46.     /* structure of fileblock for DOS 2.x search/first/next */
  47.  
  48.     struct    dta2 {
  49.         char dosres[21];        /* reserved by DOS */
  50.         char att2;            /* file attribute */
  51.         int  ft2;            /* file time */
  52.         int  fd2;            /* file date */
  53.         long fs2;            /* file size */
  54.         };
  55.  
  56.     struct    dta2    *dtaptr;
  57.  
  58.     /*
  59.     -------------------------------------------------
  60.     structure of file info block from wildexp modules
  61.     -------------------------------------------------
  62.     */
  63.  
  64.     struct    fil_info {
  65.         unsigned attrib;        /* attribute found (int for future UNIX) */
  66.         unsigned ftime;         /* file time MSDOS format */
  67.         unsigned fdate;         /* file date MSDOS format */
  68.         long fsize;            /* file size */
  69.         char fil_nam[FNSZE];        /* ASCIIZ file name - maximum which is not all allocated by sbrk */
  70.         };
  71.  
  72.     struct fil_info *finfo;
  73.  
  74.     /* get current(default) drive/directory */
  75.  
  76.     segread(&segs);
  77.     regs.ds = segs.dss;
  78.     cur_dir[0] = bdos(25,0) + 65;        /* get default drive */
  79.     cur_dir[1] = '\0';
  80.     strcat(cur_dir,":\\");
  81.     strcpy(root_dir,cur_dir);
  82.     regs.si = (unsigned) &cur_dir[3];    /* only for small model */
  83.     regs.dx = 0;
  84.     regs.bx = 0;
  85.     regs.ax = 0x4700;
  86.     sysint21(®s, ®s);
  87.     if(strlen(cur_dir) > 3)
  88.         strcat(cur_dir,"\\");
  89.     bdos(26,dta);                /* set DTA */
  90.     oargv = *oargvp;
  91.     oargc = *oargcp;
  92.     nargc = 1;
  93.  
  94.     if ((nargv = (char **) sbrk(MAXITEMS * 2 + 2)) == (char *) -1)
  95.         return ERROR;
  96.  
  97.     dtaptr = (struct dta2 *) &dta[0];
  98.     if((new_c = oargc) == 1)
  99.         new_c++;
  100.     for (i = 1; i < new_c; i++)
  101.         {
  102.         if(oargc != 1)
  103.             {
  104.             j =0;
  105.             while(( c = oargv[i][j]) != '\0')
  106.                 fd[j++] = ( c == '/') ? '\\' : c;
  107.             fd[j] = '\0';
  108.             }
  109.         else
  110.             strcpy(fd,"*.*");
  111.         if(strcmp(fd,"\\") == 0)
  112.             strcpy(fd, root_dir);
  113.         if(strrchr(fd,'\\') == NULL && fd[1] == ':')
  114.             {
  115.             strncpy(tmpfn,fd,2);
  116.             tmpfn[2] = '\0';
  117.             strcat(tmpfn,"\\");
  118.             if(strlen(fd) == 2)
  119.                 strcat(tmpfn, "*.*");
  120.             else
  121.                 strcat(tmpfn, &fd[2]);
  122.             }
  123.         else if(strrchr(fd,'\\') == NULL && fd[1] != ':')
  124.             {
  125.             strcpy(tmpfn,cur_dir);
  126.             strcat(tmpfn,fd);
  127.             }
  128.         else
  129.             strcpy(tmpfn, fd);
  130.         if(strlen(tmpfn) == 3 && tmpfn[1] == ':' && tmpfn[2] == '\\')
  131.             strcat(tmpfn,"*.*");
  132.         tmpfnp = strrchr(tmpfn, '\\') + 1;
  133.         strcpy(fd,tmpfn);
  134.         first_time = TRUE;
  135.         while (1)              /* find all matching files */
  136.             {
  137.             bdos(26,dta);        /* set DTA */
  138.             dmapos = (first_time) ? srchfst(fd, atts) : srchnxt();
  139.             if (dmapos == -1)
  140.                 break;
  141.             if(first_time && (dtaptr->att2 & 0x10) && strrchr(fd,'*') == NULL && strrchr(fd,'?') == NULL)
  142.                 {
  143.                 strcat(fd,"\\");
  144.                 strcat(fd,"*.*");
  145.                 strcpy(tmpfn, fd);
  146.                 tmpfnp = strrchr(tmpfn,'\\') + 1;
  147.                 continue;
  148.                 }
  149.             first_time = FALSE;
  150.             hknam2(tmpfnp,dta);
  151.             if ((nargv[nargc] = sbrk((sizeof(struct fil_info)-FNSZE)+strlen(tmpfn) + 1)) == (char *) -1)
  152.                 return ERROR;
  153.             finfo = (struct fil_info *) nargv[nargc];
  154.             strcpy(finfo->fil_nam,tmpfn);
  155.             finfo->attrib = dtaptr->att2;
  156.             finfo->ftime = dtaptr->ft2;
  157.             finfo->fdate = dtaptr->fd2;
  158.             finfo->fsize = dtaptr->fs2;
  159.             nargc++;
  160.             }
  161.         }
  162.     *oargcp = nargc;
  163.     *oargvp = nargv;
  164.     return 0;
  165. }
  166.  
  167.  
  168.  
  169. hknam2(dest,source)
  170. char *dest, *source;
  171. {
  172.     int i,j,k;
  173.  
  174.     j = 0;
  175.     for (i = 30; i < 45; i++)
  176.     {
  177.         if (source[i] == '\0') break;
  178.         dest[j++] = source[i];
  179.     }
  180.     dest[j] = '\0';
  181. }
  182.  
  183. srchfst(fdesc, attributes)
  184. char *fdesc;
  185. int attributes;
  186. {
  187.     struct regval regs;
  188.     struct sval segs;
  189.     int status;
  190.  
  191.     segread(&segs);
  192.     regs.ds = segs.dss;
  193.     regs.ax = 0x4e00;
  194.     regs.bx = 0;
  195.     regs.cx = attributes & 0x17;        /* mask out naughty bits */
  196.     regs.dx = (unsigned) fdesc;
  197.     status = sysint21(®s, ®s);
  198.     if(regs.ax == 2 || regs.ax == 18)
  199.         return(-1);
  200.     else
  201.         return(0);
  202. }
  203.  
  204. srchnxt()
  205. {
  206.     struct regval regs;
  207.     int status;
  208.  
  209.     regs.ax = 0x4f00;
  210.     regs.bx =0;
  211.     regs.cx = 0;
  212.     regs.dx = 0;
  213.     status = sysint21(®s, ®s);
  214.     if(regs.ax == 18 || regs.ax == 2)
  215.         return(-1);
  216.     else
  217.         return(0);
  218. }
  219.  
  220.